import { Message } from 'element-ui'
import router from './router'
import store from './store'
// import { removeToken } from './utils/auth'

const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  const _token = store.getters.token
  if (_token) {
    if (store.getters.isLogin && to.path === '/login') {
      return next('/')
    }
    if (store.getters.isLogin) {
      return next()
    }
    try {
      await store.dispatch('user/getUserInfo')
      await store.dispatch('permission/filterRoutes')
      // if (to.path === '/login') {
      //   return next('/')
      // }
      return next(to)
    } catch (error) {
      store.dispatch('user/logout')
      Message.error('请重新登录！')
      if (to.path === '/login') {
        return next()
      }
      return next('/login?redirect=' + to.fullPath)
    }

    // if (to.path === '/login') {
    //   return next('/')
    // }
    // return next()
  }
  if (whiteList.includes(to.path)) {
    return next()
  }
  return next('/login')
})
